using UnityEngine;
using UnityEngine.UI;

public class RightJoystickTouchContoller : MonoBehaviour
{
	public Image rightJoystickBackgroundImage;

	public bool rightJoyStickAlwaysVisible;

	private Image rightJoystickHandleImage;

	private RightJoystick rightJoystick;

	private int rightSideFingerID;

	private void Start()
	{
		if (rightJoystickBackgroundImage.GetComponent<RightJoystick>() == null)
		{
			Debug.LogError("There is no RightJoystick script attached to Right Joystick game object.");
		}
		else
		{
			rightJoystick = rightJoystickBackgroundImage.GetComponent<RightJoystick>();
			rightJoystickBackgroundImage.enabled = rightJoyStickAlwaysVisible;
		}
		if (rightJoystick.transform.GetChild(0).GetComponent<Image>() == null)
		{
			Debug.LogError("There is no right joystick handle attached to this script.");
			return;
		}
		rightJoystickHandleImage = rightJoystick.transform.GetChild(0).GetComponent<Image>();
		rightJoystickHandleImage.enabled = rightJoyStickAlwaysVisible;
	}

	private void Update()
	{
	}

	private void FixedUpdate()
	{
		if (Input.touchCount <= 0)
		{
			return;
		}
		Touch[] touches = Input.touches;
		for (int i = 0; i < Input.touchCount; i++)
		{
			if (touches[i].phase == TouchPhase.Began && touches[i].position.x > (float)(Screen.width / 2))
			{
				rightSideFingerID = touches[i].fingerId;
				if (!rightJoystick.joystickStaysInFixedPosition)
				{
					Vector3 position = rightJoystickBackgroundImage.rectTransform.position;
					position.x = touches[i].position.x + rightJoystickBackgroundImage.rectTransform.sizeDelta.x / 2f;
					position.y = touches[i].position.y - rightJoystickBackgroundImage.rectTransform.sizeDelta.y / 2f;
					position.x = Mathf.Clamp(position.x, (float)(Screen.width / 2) + rightJoystickBackgroundImage.rectTransform.sizeDelta.x, Screen.width);
					position.y = Mathf.Clamp(position.y, 0f, (float)Screen.height - rightJoystickBackgroundImage.rectTransform.sizeDelta.y);
					rightJoystickBackgroundImage.rectTransform.position = position;
					rightJoystickBackgroundImage.enabled = true;
					rightJoystickBackgroundImage.rectTransform.GetChild(0).GetComponent<Image>().enabled = true;
				}
				else if (touches[i].position.x <= rightJoystickBackgroundImage.rectTransform.position.x && touches[i].position.x >= rightJoystickBackgroundImage.rectTransform.position.x - rightJoystickBackgroundImage.rectTransform.sizeDelta.x && touches[i].position.y >= rightJoystickBackgroundImage.rectTransform.position.y && touches[i].position.y <= rightJoystickBackgroundImage.rectTransform.position.y + rightJoystickBackgroundImage.rectTransform.sizeDelta.y)
				{
					rightJoystickBackgroundImage.enabled = true;
					rightJoystickBackgroundImage.rectTransform.GetChild(0).GetComponent<Image>().enabled = true;
				}
			}
			if (touches[i].phase == TouchPhase.Ended && touches[i].fingerId == rightSideFingerID)
			{
				rightJoystickBackgroundImage.enabled = rightJoyStickAlwaysVisible;
				rightJoystickHandleImage.enabled = rightJoyStickAlwaysVisible;
			}
		}
	}
}
